package com.cgt.web.servlet;

import com.cgt.web.pojo.Banji;
import com.cgt.web.service.IBanjiService;
import com.cgt.web.service.impl.BanjiServiceImpl;
import com.cgt.web.util.PageInfo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/banji")//重命名
public class BanjiServlet extends HttpServlet {
    //private IBanjiDao banjiDao = new BanjiDaoImpl();
    private IBanjiService banjiService = new BanjiServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");//设置UTF-8编码

//        HttpSession session = req.getSession();
//        User user = (User) session.getAttribute("user");
//        if(user == null){
//            resp.sendRedirect("/login.jsp");
//            return;
//        }

        String method = req.getParameter("method");//获取method参数，参数类型是String
        if(method == null || method.equals("")){//判断method为空，重定向为selectAll
            method = "selectByPage";
        }
        switch (method){//根据method获取到的命令选择相应的方法
            case "selectAll"://查
                selectAll(req,resp);
                break;
            case "selectByPage":
                selectByPage(req,resp);
                break;
            case "deleteById"://删
                deleteById(req,resp);
                break;
            case "addInf"://增
                addInf(req,resp);
                break;
            case "toUpdate"://改1获取id对应数据
                toUpdate(req,resp);
                break;
            case "update"://改2输出对应数据并进行修改保存
                update(req,resp);
                break;
        }
    }

    //banji?method=selectByPage&pageNo=1&pageSize=5
    private void selectByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("BanjiServlet.selectByPage");

        String pageNo = req.getParameter("pageNo");
        if(pageNo == null || pageNo.equals("")){
            pageNo = "1";
        }
        String pageSize = req.getParameter("pageSize");
        if(pageSize == null || pageSize.equals("")){
            pageSize = "5";
        }

        PageInfo<Banji> pageInfo = banjiService.selectByPage(Integer.parseInt(pageNo),Integer.parseInt(pageSize));

        req.setAttribute("pageInfo",pageInfo);
        req.getRequestDispatcher("banji_list.jsp").forward(req,resp);
    }

    private void selectAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("BanjiServlet.selectAll");

        List<Banji> list = banjiService.selectAll();

        req.setAttribute("list",list);//转发
        req.getRequestDispatcher("banji_list2.jsp").forward(req,resp);
    }

    private void deleteById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("BanjiServlet.deleteById");

        String id = req.getParameter("id");
        banjiService.deleteById(Integer.parseInt(id));//将获取的String类型的id转化为int类型

        resp.sendRedirect("/banji?method=selectByPage");//重定向
    }

    private void addInf(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("BanjiServlet.addInf");

        String name = req.getParameter("name");
        String address = req.getParameter("address");

        Banji banji = new Banji();
        banji.setName(name);
        banji.setAddress(address);

        banjiService.addInf(banji);

        resp.sendRedirect("/banji?method=selectByPage");
    }

    private void toUpdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("BanjiServlet.toUpdate");

        String id = req.getParameter("id");

        Banji banji = banjiService.toUpdate(Integer.parseInt(id));

        req.setAttribute("banji",banji);//在请求中设置"banji"属性的值为banji
        req.getRequestDispatcher("banji_update.jsp").forward(req,resp);
        //请求转发到banji_update.jsp
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("BanjiServlet.update");

        String id = req.getParameter("id");
        String name = req.getParameter("name");
        String address = req.getParameter("address");
        Banji banji = new Banji(Integer.parseInt(id),name,address);

        banjiService.update(banji);

        resp.sendRedirect("/banji?method=selectByPage");
    }
}
